package com.woniu.punishcomplaintnotice.controller;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.woniu.common.entity.Adopt;
import com.woniu.common.entity.AdoptApply;
import com.woniu.common.entity.PageResult;
import com.woniu.common.entity.ResponseResult;
import com.woniu.punishcomplaintnotice.service.AdoptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

//@CrossOrigin
@RestController
public class AdoptController {
    @Autowired
    AdoptService adoptService;

    //修改领养状态
    @RequestMapping("updateStatus")
    public ResponseResult<String> updateStatus(Integer id){

        if (adoptService.updateStatus(id)) {

            return new ResponseResult<>(500, null, "修改成功", null, null);

        }
        return new ResponseResult<>(500, null, "修改失败", null, null);
    }

    //领养申请
    @PostMapping("/apply")
    public ResponseResult<String> addAdoptApply(@RequestBody AdoptApply adoptApply){

        if (adoptService.addAdoptApply(adoptApply)){

            return new ResponseResult<>(200, null, "已发出申请，请敬候佳音", null, null);

        }
        return new ResponseResult<>(500, null, "申请失败，请考虑后再次发出申请", null, null);
    }

    //查询领养信息
    @RequestMapping("/findAllAdopt")
    public PageResult<Adopt> findAllAdopt(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {

        return adoptService.findAllAdopt(page, size);
    }

    @RequestMapping(value = "uploadAdopt", method = RequestMethod.POST)
    public void uploadAdopt(HttpServletRequest request, @RequestParam MultipartFile file) {

        String age = request.getParameter("age");
        String sex = request.getParameter("sex");
        String longs = request.getParameter("long");
        String heigh = request.getParameter("heigh");
        String weight = request.getParameter("weight");
        String types = request.getParameter("type");

        String url = null;
        // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1（杭州）为例，Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
        String endpoint = "oss-cn-chengdu.aliyuncs.com";
        // 阿里云账号AccessKey拥有所有API的访问权限，风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维，请登录RAM控制台创建RAM用户。
        String accessKeyId = "LTAI5tRST65ssEbSEwikZfCN";
        String accessKeySecret = "dqc3VgpVkBnEx9q7iQ8bNpXt36iutI";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        try {
            // 填写本地文件的完整路径。如果未指定本地路径，则默认从示例程序所属项目对应本地路径中上传文件流。
            InputStream inputStream = file.getInputStream();
            //照片或者文件名字
            String filename = file.getOriginalFilename();
            //文件重名
            filename = UUID.randomUUID().toString();
            //文件打散
            DateFormat df = new SimpleDateFormat("yyyy/MM/dd");
            String strDate = df.format(new Date());
            filename = strDate + "/" + filename;
            // 填写Bucket名称和Object完整路径。Object完整路径中不能包含Bucket名称。
            ossClient.putObject("poppyflower", filename, inputStream);
            // 关闭OSSClient。
            ossClient.shutdown();
            //返回图片或者文件在OSS里面的路径
            //https://poppyflower.oss-cn-chengdu.aliyuncs.com/DogRegister/flowChart.png
            url = "https://poppyflower.oss-cn-chengdu.aliyuncs.com/" + filename;
            /*responseResult.setCode(200);
            responseResult.setMessage("上传成功");
            responseResult.setData(url);*/

        } catch (IOException e) {
            e.printStackTrace();
        }
        Adopt adopt = new Adopt();
        adopt.setSex(sex);
        adopt.setAge(age);
        adopt.setHeight(heigh);
        adopt.setLongs(longs);
        adopt.setPhoto(url);
        adopt.setWeight(weight);
        adopt.setType(types);
        adopt.setStatus(1);
        adoptService.insertInfo(adopt);


    }
}
